<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="HIS功能名称" prop="hisName">
      <el-input v-model="dataForm.hisName" placeholder="HIS功能名称"></el-input>
    </el-form-item>
    <el-form-item label="HIS查询键" prop="hisKey">
      <el-input v-model="dataForm.hisKey" placeholder="HIS查询键"></el-input>
    </el-form-item>
    <el-form-item label="查询类型" prop="hisType">
      <el-radio-group v-model="dataForm.hisType" @change="changeHisType">
        <el-radio :label="1">部门</el-radio>
        <el-radio :label="2">单位</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- <el-form-item label="部门/单位id" prop="checkId">
      <el-input v-model="dataForm.checkId" placeholder="部门/单位id"></el-input>
    </el-form-item> -->
    <el-row >
      <el-col :span="21">
        <el-form-item label="查询部门" v-if="dataForm.hisType == 1">
          <el-input type="textarea" v-model="dataForm.checkNames" disabled></el-input>
        </el-form-item>
        <el-form-item label="查询单位" v-if="dataForm.hisType == 2">
          <el-input type="textarea" v-model="dataForm.checkNames" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label-width="0">
          <el-button size="mini" @click="openDeptDialog">选择</el-button>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="HIS接口路径/值" prop="hisValue">
      <el-input v-model="dataForm.hisValue" placeholder="HIS接口路径/值"></el-input>
    </el-form-item>
    <el-form-item label="HIS编码" prop="hisCode">
      <el-input v-model="dataForm.hisCode" placeholder="HIS编码"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-radio-group v-model="dataForm.status">
        <el-radio :label="1">启用</el-radio>
        <el-radio :label="0">停用</el-radio>
      </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗 部门列表 -->
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
    <!-- 弹窗 单位列表 -->
    <search-unit v-if="unitVisible" ref="unitDialog" @refreshData="getDeptData"></search-unit>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/eims/hisparam'
    import SearchDept from '../setmeal/SearchDept'
    import SearchUnit from './SearchUnit'
    export default {
    data () {
      return {
        visible: false,
        deptVisible: false,
        unitVisible: false,
        dataForm: {
          id: 0,
          hisName: '',
          hisKey: '',
          hisType: 1,
          checkId: '',
          checkName: '',
          hisValue: '',
          status: 1,
          hisCode: '',
          checkIds: [],
          checkNames: []
        },
        dataRule: {
          hisName: [
            { required: true, message: 'HIS功能名称不能为空', trigger: 'blur' }
          ],
          hisKey: [
            { required: true, message: 'HIS查询键不能为空', trigger: 'blur' }
          ],
          hisType: [
            { required: true, message: '查询类型(1-部门,2-单位)不能为空', trigger: 'blur' }
          ],
          checkId: [
            { required: true, message: '部门/单位id不能为空', trigger: 'blur' }
          ],
          hisValue: [
            { required: true, message: 'HIS接口路径/值不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      SearchDept,
      SearchUnit
    },
    created() {
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        if(this.dataForm.id == 0){
          this.dataForm.checkNames = []
          this.dataForm.checkIds = []
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if(this.dataForm.checkIds.length==0){
          this.$message.warning("请选择相应部门/单位")
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                });
            } else {
                addObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                })
            }
          }
        })
      },
      //  打开查询部门/单位弹窗
      openDeptDialog() {
        if(this.dataForm.hisType == 1){
          this.deptVisible = true
          this.$nextTick(() => {
            this.$refs.deptDialog.init()
          })
        }else{
          this.unitVisible = true
          this.$nextTick(() => {
            this.$refs.unitDialog.init()
          })
        }
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if (data.length) {
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.dataForm.checkNames = names
        this.dataForm.checkIds = ids
      },
      //
      changeHisType(){
        this.dataForm.checkNames = []
      }
    }
  }
</script>
